package com.cw.jeeyt.controller.process;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.StringUtils;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.process.ProcessLeaveBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.process.LeaveService;
import com.cw.jeeyt.service.intf.process.ProcessLeaveService;

/**
 * 请假Controller
 *
 * @author liuruijun
 * @version 2018-04-05
 */
@Controller
@RequestMapping(value = "/oa/leave")
public class LeaveController extends BaseController {

    @Autowired
    protected LeaveService leaveService;
    @Autowired
    private ProcessLeaveService processLeaveService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dateFormat.setLenient(false);
        //true:允许输入空值，false:不能为空值
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @ModelAttribute
    public ProcessLeaveBo get(@RequestParam(required = false) String id) throws Exception {
        ProcessLeaveBo leave = null;
        if (StringUtils.isNotBlank(id)) {
            leave = processLeaveService.getProcessLeaveById(id);
        }
        if (leave == null) {
            leave = new ProcessLeaveBo();
        }
        return leave;
    }

    @RequestMapping(value = {"form"})
    public String form(ProcessLeaveBo leave, Model model) {

        String view = "leave_form";
        if (StringUtils.isNotBlank(leave.getId())) {
            String taskDefKey = leave.getAct()
                .getTaskDefKey();

            // 查看工单
            if (leave.getAct()
                .isFinishTask()) {
                view = "leave_view";
            }
            // 部门领导审核环节
            else if ("deptLeaderAudit".equals(taskDefKey)) {
                view = "leave_audit";
            }
            // 人事审核环节
            else if ("hrAudit".equals(taskDefKey)) {
                view = "leave_audit";
            }
            // 调整申请
            else if ("modifyApply".equals(taskDefKey)) {
                view = "leave_form";
            }
        }
        model.addAttribute("leave", leave);
        model.addAttribute("permission", new PermissionDirective());
        model.addAttribute("PageDict", new PageDictDirective());
        return "process/business/" + view;
    }

    /**
     * 启动请假流程
     *
     * @param leave
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultDto save(ProcessLeaveBo leave) {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            Map<String, Object> variables = Maps.newHashMap();
            String message = leaveService.save(leave, variables);
            jsonResultDto.setSuccess(true);
            jsonResultDto.setMsg(message);
        } catch (Exception e) {
            jsonResultDto.setMsg("系统内部错误！");
            e.printStackTrace();
        }
        return jsonResultDto;
    }

    /**
     * 工单执行（完成任务）
     *
     * @param leave
     * @return
     */
    @RequestMapping(value = "saveAudit")
    @ResponseBody
    public JsonResultDto saveAudit(ProcessLeaveBo leave) throws Exception {
        String message = leaveService.auditSave(leave);
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        jsonResultDto.setSuccess(true);
        jsonResultDto.setMsg(message);
        return jsonResultDto;
    }
}
